package com.rednose.videoplayer.adapters;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.rednose.videoplayer.model.MediaFiles;
import com.rednose.videoplayer.R;
import com.rednose.videoplayer.activities.VideoFilesActivity;

import java.util.ArrayList;

public class VideoFoldersAdapter extends RecyclerView.Adapter<VideoFoldersAdapter.ViewHolder> {
    private ArrayList<MediaFiles> mediaFiles;
    private ArrayList<String> folderPath;
    private Context context;

    //构造函数，将视频文件列表、文件夹路径列表和上下文传入
    public VideoFoldersAdapter(ArrayList<MediaFiles> mediaFiles, ArrayList<String> folderPath, Context context) {
        this.mediaFiles = mediaFiles;
        this.folderPath = folderPath;
        this.context = context;
    }

    //这个方法用于创建ViewHolder实例
    @NonNull
    @Override
    public VideoFoldersAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //加载布局
        //LayoutInflater是用来找res/layout/下的xml布局文件，然后设置给view，后面两个参数分别是父布局和是否立即加载
        View view = LayoutInflater.from(context).inflate(R.layout.folder_item, parent, false);
        return new ViewHolder(view);//返回ViewHolder实例
    }

    //创建一个新的ViewHolder实例对应一个folder_item布局，将数据绑定到ViewHolder上
    @Override
    public void onBindViewHolder(@NonNull VideoFoldersAdapter.ViewHolder holder, int position) {
        //获取文件夹的名字
        int indexPath = folderPath.get(position).lastIndexOf("/");//获取最后一个"/"的位置
        String nameOFFolder = folderPath.get(position).substring(indexPath + 1);//文件夹的名字就是最后一个"/"后面的字符串
        //设置文件夹名字
        holder.folderName.setText(nameOFFolder);
        //设置文件夹路径
        holder.folder_path.setText(folderPath.get(position));
        //设置文件夹内的视频数量
        holder.noOfFiles.setText(noOfFoles(folderPath.get(position)) + " 个视频");
//        holder.noOfFiles.setText("0 Videos");

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, VideoFilesActivity.class);
                intent.putExtra("folderName", nameOFFolder);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() { return folderPath.size(); }

    //ViewHolder是一个内部类，用于对控件的实例进行缓存
    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView folderName, folder_path, noOfFiles;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            folderName = itemView.findViewById(R.id.folderName);
            folder_path = itemView.findViewById(R.id.folderPath);
            noOfFiles = itemView.findViewById(R.id.noOfFiles);
        }
    }

    //又遍历一遍所有的视频文件，计算文件夹的视频文件数量
    int noOfFoles(String folder_name) {
        int files_no = 0;
        for (MediaFiles mediaFiles : mediaFiles) {
            if (mediaFiles.getPath().substring(0, mediaFiles.getPath().lastIndexOf("/"))
                    .endsWith(folder_name)) {
                files_no++;
            }
        }
        return files_no;
    }
}
